<template>
  <div class="Comment" :style="{ width: commentWidth }" v-my-loading="loading">
    <div style="display: flex">
      <el-avatar
        size="large"
        :src="user.avatar ? user.avatar : 'static/img/tou.jpg' "
        style="margin: 0 10px"
      />
      <div style="flex: auto">
        <el-input
          type="textarea"
          :autosize="{ minRows: minRows, maxRows: maxRows }"
          :placeholder="placeholder"
          v-model="textareaMap['initComment']"
        ></el-input>

        <div>
          <div style="margin: 10px 0">
            <el-button @click="pBodyStatus('initComment')">Emoji表情</el-button>
            <el-button
              plain
              type="primary"
              @click="doSendRoot('initComment')"
              style="float: right"
            >发送</el-button
            >
          </div>
          <div class="OwO" v-if="pBodyMap['initComment']">
            <div class="OwO-body">
              <ul class="OwO-items">
                <li
                  class="OwO-item"
                  v-for="(oitem, index) in OwOlist"
                  :key="index"
                  @click="chooseEmoji('initComment', oitem.title)"
                >
                  <img :src="'/static/emot/image/'+oitem.url" alt />
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div
      style="
        font-size: 20px;
        margin: 20px 5px;
        border-left: 5px solid #3cb371;
        padding-left: 10px;
      "
    >
      <h4>评论列表 ({{ commentNum }}条)</h4>
    </div>

    <div v-for="(item, index) in commentList" :key="index" style="overflow: hidden">
      <div style="display: flex">
        <el-avatar
          size="large"
          style="margin: 0 10px;min-width: 40px"
          :src="item.createUser.avatar ? item.createUser.avatar : 'static/img/tou.jpg'"
        ></el-avatar>

        <div style="flex: auto">
          <div style="display: inline-block">
            <strong style="font-size: 16px; margin-right: 10px">{{
              item.createUser.nickName ? item.createUser.nickName : '用户已注销'
              }}</strong>
          </div>
          <div class="date">{{ item.createTime | formatDate}}</div>

          <div class="reply-content">
            <div v-html="analyzeEmoji(item.content)" style="line-height: 25px">
              {{ analyzeEmoji(item.content) }}
            </div>
            <div>
              <el-popconfirm
                title="确定要删除该评论吗？"
                @confirm="deleteRootComment(item.id, index)"
              >
                <span
                  slot="reference"
                  class="deleteComment"
                  v-show="item.createUser.id === user.id"
                ><i class="el-icon-delete" /> 删除</span
                >
              </el-popconfirm>
              <div
                class="reply-font"
                v-if="!replyMap[item.id]"
                @click="doReply(item.id)"
              >
                <i class="el-icon-chat-square"></i>
                回复
              </div>
              <div class="reply-font" v-else @click="cancel(item.id)">
                <i class="el-icon-chat-square"></i>
                取消回复
              </div>
            </div>
            <div v-if="replyMap[item.id]">
              <el-input
                type="textarea"
                :autosize="{ minRows: minRows, maxRows: maxRows }"
                :placeholder="placeholder"
                v-model="textareaMap[item.id]"
              ></el-input>

              <div>
                <div style="margin: 10px 0">
                  <el-button @click="pBodyStatus(item.id)">Emoji表情</el-button>
                  <el-button
                    @click="doSend(item.id, item.createUser.id, item.id, index)"
                    style="float: right"
                    plain
                    type="primary"
                  >发送</el-button
                  >
                </div>
                <div v-if="pBodyMap[item.id]">
                  <div class="OwO">
                    <div class="OwO-body">
                      <ul class="OwO-items">
                        <li
                          class="OwO-item"
                          v-for="(oitem, index) in OwOlist"
                          :key="'oitem' + index"
                          @click="chooseEmoji(item.id, oitem.title)"
                        >
                          <img
                            :src="'/static/emot/image/' + oitem.url"
                            alt
                          />
                        </li>
                      </ul>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div>
      <el-collapse-transition
        v-for="(ritem, jndex) in item.childrenList"
        :key="jndex"
      >
        <div style="display: flex" v-show="showMap[index]" class="children-item-list">
          <el-avatar
            size="large"
            style="margin: 0 10px;min-width: 40px"
            :src="ritem.createUser.avatar ? ritem.createUser.avatar : 'static/img/tou.jpg'"
          ></el-avatar>

          <div style="flex: auto">
            <div style="display: inline-block">
              <strong style="font-size: 12px; margin-right: 10px">{{
                ritem.createUser.nickName ? ritem.createUser.nickName : '用户已注销'
                }} <i class="el-icon-caret-right"/> {{ ritem.commentUser.nickName ? ritem.commentUser.nickName : '用户已注销' }}</strong>
            </div>
            <div class="date">{{ ritem.createTime | formatDate }}</div>

            <div class="reply-content">
              <div>
                <span
                  v-html="analyzeEmoji(ritem.content)"
                  style="line-height: 25px"
                >{{ analyzeEmoji(ritem.content) }}</span
                >
              </div>
              <div>
                <el-popconfirm
                  title="确定要删除该评论吗？"
                  @confirm="deleteComment(ritem.id , index , jndex)"
                >
                  <span
                    slot="reference"
                    class="deleteComment"
                    v-show="
                      ritem.createUser.id === user.id"
                  ><i class="el-icon-delete" /> 删除</span
                  >
                </el-popconfirm>
                <div
                  class="reply-font"
                  v-if="!replyMap[ritem.id]"
                  @click="doReply(ritem.id)"
                >
                  <i class="el-icon-chat-square"></i>
                  回复
                </div>
                <div class="reply-font" v-else @click="cancel(ritem.id)">
                  <i class="el-icon-chat-square"></i>
                  取消回复
                </div>
              </div>

              <div v-if="replyMap[ritem.id]">
                <el-input
                  type="textarea"
                  :autosize="{ minRows: minRows, maxRows: maxRows }"
                  :placeholder="placeholder"
                  v-model="textareaMap[ritem.id]"
                ></el-input>

                <div>
                  <div style="margin: 10px 0">
                    <el-button @click="pBodyStatus(ritem.id)"
                    >Emoji表情</el-button
                    >
                    <el-button
                      @click="doSend(ritem.id, ritem.commentUser.id, item.id, index)"
                      style="float: right"
                      plain
                      type="primary"
                    >发送</el-button
                    >
                  </div>
                  <div v-if="pBodyMap[ritem.id]">
                    <div class="OwO">
                      <div class="OwO-body">
                        <ul class="OwO-items">
                          <li
                            class="OwO-item"
                            v-for="(oitem, index) in OwOlist"
                            :key="'oitem' + index"
                            @click="chooseEmoji(ritem.id, oitem.title)"
                          >
                            <img :src="'/static/emot/image/'+oitem.url" alt/>
                          </li>
                        </ul>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </el-collapse-transition>
      </div>
      <div class="more-children" >
        <span
          v-if="(item.childrenCount > childrenCountMap[index] || childrenCountMap[index] === undefined) && item.childrenCount > 0"
          @click="openMoreComment(index , item.id)"
        >展开{{item.childrenCount - (childrenCountMap[index] ? childrenCountMap[index] : 0 )}}条评论<i class="el-icon-arrow-down"/>
        </span>
        <span v-else-if="item.childrenCount > 0 && showMap[index] " @click="showMap[index] = !showMap[index]">收起<i class="el-icon-arrow-up"/> </span>
        <span v-else-if="item.childrenCount > 0 && !showMap[index] " @click="showMap[index] = !showMap[index]">展开<i class="el-icon-arrow-down"/></span>
      </div>
    </div>
    <div class="comment-more">
      <h1 v-show='hasMore' class="tcolors-bg" @click="addMoreFun" >查看更多</h1>
      <h1 v-show='!hasMore' class="tcolors-bg" >没有更多</h1>
    </div>
  </div>
</template>
<script>

  import {getDateDiff, getDateTimeStamp} from "../utils/sg";

  export default {
    name:'Comment',
    props: {
      placeholder: {
        type: String,
        default: "说点什么吧",
      },
      user:{
        type: Object,
        default: function(){
          return {
            id: '',
            avatar: '',
            userName: '',
            nickName: ''
          }
        }
      },
      minRows: {
        type: Number,
        default: 4,
      },
      maxRows: {
        type: Number,
        default: 8,
      },
      commentNum: {
        type: Number,
        default: 2,
      },
      label: {
        type: String,
        default: "作者",
      },
      commentList: {
        type: Array,
        default: () => [],
      },
      commentWidth: {
        type: String,
        default: "50%",
      },
      childrenCountMap: {
        type: Array,
        default: () => [],
      }
    },
    data() {
      return {
        roleType: "",
        replyMap: {},
        pBodyMap: { initComment: false },
        textareaMap: { initComment: "" },
        showMap: {},
        loading: false,
        OwOlist: [
          //表情包和表情路径
          { title: "微笑", url: "weixiao.gif" },
          { title: "嘻嘻", url: "xixi.gif" },
          { title: "哈哈", url: "haha.gif" },
          { title: "可爱", url: "keai.gif" },
          { title: "可怜", url: "kelian.gif" },
          { title: "挖鼻", url: "wabi.gif" },
          { title: "吃惊", url: "chijing.gif" },
          { title: "害羞", url: "haixiu.gif" },
          { title: "挤眼", url: "jiyan.gif" },
          { title: "闭嘴", url: "bizui.gif" },
          { title: "鄙视", url: "bishi.gif" },
          { title: "爱你", url: "aini.gif" },
          { title: "泪", url: "lei.gif" },
          { title: "偷笑", url: "touxiao.gif" },
          { title: "亲亲", url: "qinqin.gif" },
          { title: "生病", url: "shengbing.gif" },
          { title: "太开心", url: "taikaixin.gif" },
          { title: "白眼", url: "baiyan.gif" },
          { title: "右哼哼", url: "youhengheng.gif" },
          { title: "左哼哼", url: "zuohengheng.gif" },
          { title: "嘘", url: "xu.gif" },
          { title: "衰", url: "shuai.gif" },
          { title: "吐", url: "tu.gif" },
          { title: "哈欠", url: "haqian.gif" },
          { title: "抱抱", url: "baobao.gif" },
          { title: "怒", url: "nu.gif" },
          { title: "疑问", url: "yiwen.gif" },
          { title: "馋嘴", url: "chanzui.gif" },
          { title: "拜拜", url: "baibai.gif" },
          { title: "思考", url: "sikao.gif" },
          { title: "汗", url: "han.gif" },
          { title: "困", url: "kun.gif" },
          { title: "睡", url: "shui.gif" },
          { title: "钱", url: "qian.gif" },
          { title: "失望", url: "shiwang.gif" },
          { title: "酷", url: "ku.gif" },
          { title: "色", url: "se.gif" },
          { title: "哼", url: "heng.gif" },
          { title: "鼓掌", url: "guzhang.gif" },
          { title: "晕", url: "yun.gif" },
          { title: "悲伤", url: "beishang.gif" },
          { title: "抓狂", url: "zhuakuang.gif" },
          { title: "黑线", url: "heixian.gif" },
          { title: "阴险", url: "yinxian.gif" },
          { title: "怒骂", url: "numa.gif" },
          { title: "互粉", url: "hufen.gif" },
          { title: "书呆子", url: "shudaizi.gif" },
          { title: "愤怒", url: "fennu.gif" },
          { title: "感冒", url: "ganmao.gif" },
          { title: "心", url: "xin.gif" },
          { title: "伤心", url: "shangxin.gif" },
          { title: "猪", url: "zhu.gif" },
          { title: "熊猫", url: "xiongmao.gif" },
          { title: "兔子", url: "tuzi.gif" },
          { title: "喔克", url: "ok.gif" },
          { title: "耶", url: "ye.gif" },
          { title: "棒棒", url: "good.gif" },
          { title: "不", url: "no.gif" },
          { title: "赞", url: "zan.gif" },
          { title: "来", url: "lai.gif" },
          { title: "弱", url: "ruo.gif" },
          { title: "草泥马", url: "caonima.gif" },
          { title: "神马", url: "shenma.gif" },
          { title: "囧", url: "jiong.gif" },
          { title: "浮云", url: "fuyun.gif" },
          { title: "给力", url: "geili.gif" },
          { title: "围观", url: "weiguan.gif" },
          { title: "威武", url: "weiwu.gif" },
          { title: "话筒", url: "huatong.gif" },
          { title: "蜡烛", url: "lazhu.gif" },
          { title: "蛋糕", url: "dangao.gif" },
          { title: "发红包", url: "fahongbao.gif" },
        ],
      };
    },
    filters: {
      formatDate(value) {
        return getDateDiff(value)
      },
    },
    computed: {
      hasMore (){
        let  childrenTotal = 0;
        for (let i = 0 ; i< this.commentList.length ; i++ ) {
          childrenTotal = childrenTotal + this.commentList[i].childrenCount
        }
        return this.commentList.length < this.commentNum - childrenTotal
      }
    },
    components: {},
    mounted() {
    },
    methods: {
      openMoreComment(index, rootCommentId) {
        this.loading = true
        this.$emit('openMoreComment', index, rootCommentId , callback => {
          this.closeLoading()
        })
        this.$set(this.showMap, index ,true)
      },
      closeLoading() {
        this.loading = false
      },
      doReply(index) {
        this.$set(this.replyMap, index, true);
      },
      cancel(index) {
        this.$set(this.replyMap, index, false);
        this.$set(this.pBodyMap, index, false);
        this.$set(this.textareaMap, index, "");
      },
      deleteComment(id , index, rndex) {
        this.$emit("deleteComment", id , index, rndex);
      },
      deleteRootComment(id , index) {
        this.$emit("deleteRootComment", id , index);
      },
      doSendRoot(key) {
        this.$emit('doSendRoot', this.textareaMap[key])
        this.$set(this.textareaMap, key, "");
        this.$set(this.pBodyMap, key, false);
      },
      // 参数：评论内容,被评论用户id,父级评论id
      doSend(key, targetUser_id = "", pCommentId = "", index) {
        this.$emit("doSend", this.textareaMap[key], targetUser_id, pCommentId, index);
        this.$set(this.textareaMap, key, "");
        this.$set(this.replyMap, key, false);
        this.$set(this.pBodyMap, key, false);
      },
      addMoreFun() {
        this.loading = true
        this.$emit("addMore", callback => {
          this.closeLoading()
        })
      },
      //选择表情包
      chooseEmoji: function (index, inner) {
        var con = "";
        if (!this.textareaMap[index]) {
          this.$set(this.textareaMap, index, "");
        }
        con = this.textareaMap[index] += "[" + inner + "]";
        this.$set(this.textareaMap, index, con);
      },
      analyzeEmoji: function (cont) {
        //编译表情替换成图片展示出来
        var pattern1 = /\[[\u4e00-\u9fa5]+\]/g;
        var pattern2 = /\[[\u4e00-\u9fa5]+\]/;
        var content = cont.match(pattern1);
        var str = cont;
        if (content) {
          for (var i = 0; i < content.length; i++) {
            for (var j = 0; j < this.OwOlist.length; j++) {
              if ("[" + this.OwOlist[j].title + "]" == content[i]) {
                var src = this.OwOlist[j].url;
                break;
              }
            }
            var s = '/static/emot/image/' + src;
            var imoj = "<img src='" + s + "'/>";

            str = str.replace(pattern2, imoj);
          }
        }
        return str;
      },

      pBodyStatus(key) {
        this.$set(this.pBodyMap, key, !this.pBodyMap[key]);
      },
    },
  };
</script>

<style scoped>
  .children-item-list {
    padding: 10px 0 10px 40px;
  }
  .comment-more {
    text-align: center;
    line-height: 25px;
  }
  .Comment {
    color: black;
    padding-bottom: 50px;
  }
  .tcolors-bg {
    border-radius: 2vh;
    background: #97dffd;
    -webkit-transition: all 0.3s ease-in-out;
    transition: all 0.3s ease-in-out;
  }
  .tcolord-bg {
    background: #746faa;
  }
  .tcolors-bg:hover {
    background: #7c79ac;
  }
  .Comment .OwO {
    z-index: 1;
  }

  .Comment .OwO .OwO-body {
    position: absolute;
    width: 80%;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 0 4px 4px 4px;
  }

  .Comment .OwO .OwO-body .OwO-items {
    max-height: 197px;
    overflow-y: scroll;
    padding: 10px;
  }

  .Comment .OwO .OwO-body .OwO-items .OwO-item {
    background: #f7f7f7;
    padding: 5px 10px;
    border-radius: 5px;
    display: inline-block;
    margin: 0 10px 12px 0;
    transition: 0.3s;
    line-height: 19px;
    font-size: 20px;
    cursor: pointer;
  }

  .Comment .OwO .OwO-body .OwO-items .OwO-item:hover {
    background: #eee;
    box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
  }

  .Comment .date {
    font-size: 12px;
    color: grey;
    margin: 6px 0;
  }

  .Comment .deleteComment {
    font-size: 14px;
    color: #6d5858;
    cursor: pointer;
  }
  .Comment .more-children {
    font-size: 14px;
    font-weight: bold;
    color: #48456D;
    cursor: pointer;
    display: inline-block;
    padding-top: 10px;
    padding-left: 50px;
    margin-bottom: 20px;
  }
  .Comment .reply-content {
    font-size: 15px;
  }

  .Comment .reply-content .reply-font {
    margin-bottom: 5px;
    color: grey;
    cursor: pointer;
    display: inline-block;
  }

</style>
